/** 
 * Copyright (C), 2010-2012, AIYOU Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:PrivateLetterServiceImp.java
 * Version:1.0
 * Date:Dec 10, 2011
 * Description: 
 * author:whh
 */
package com.tietiao.service.privateletter;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tietiao.common.constants.LogConstants;
import com.tietiao.dao.PrivateLetterMapper;
import com.tietiao.dao.model.PrivateLetter;
import com.tietiao.dao.model.SysUser;
import com.tietiao.service.syslog.SysLogService;

@Service
public class PrivateLetterServiceImp implements PrivateLetterService {
	@Resource
	private PrivateLetterMapper privateLetterMapper;
	@Resource
	private SysLogService sysLogService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#deletePrivateLetter
	 * (int, com.tietiao.dao.model.SysUser)
	 */
	@Override
	public synchronized int deletePrivateLetter(int id, SysUser user) {
		PrivateLetter letter = loadPrivateLetterId(id);
		int result = 0;
		if (letter != null) {
			// 如果出现异常，自已发送信件给自己
			if (letter.getSendId() == user.getUserId()
					&& letter.getReceiveId() == user.getUserId()) {
				letter.setSendDeleteFlag(1);
				letter.setReceiveDeleteFlag(1);
			} else if (letter.getSendId() == user.getUserId()) {
				letter.setSendDeleteFlag(1);
				sysLogService.insertSysLog(LogConstants.OP_TYPE_DELETE,
						"发件人删除信件",
						user.getUserId() + "删除信件，信件ID为：" + letter.getId());

				if (letter.getSendDeleteFlag() == 1
						&& letter.getReceiveDeleteFlag() == 1) {
					// FIXME 信件不设外键，数据库须更改
					result = privateLetterMapper.deleteByPrimaryKey(id);
				} else {
					result = privateLetterMapper.updateByPrimaryKey(letter);
				}

				// 删除的是最新消息，则需将第二新的消息的发送显示状态更新为1
				if (letter.getSendShowFlag() == 1) {
					letter.setSendShowFlag(0);
					PrivateLetter latestLetter = privateLetterMapper
							.selectLatestLetter(user.getUserId(),
									letter.getReceiveId());
					if (latestLetter != null) {
						if (latestLetter.getSendId() == user.getUserId()) {
							latestLetter.setSendShowFlag(1);
						} else {
							latestLetter.setReceiveShowFlag(1);
						}
						privateLetterMapper.updateByPrimaryKey(latestLetter);
					}
				}

			} else {
				sysLogService.insertSysLog(LogConstants.OP_TYPE_DELETE,
						"收件人删除信件",
						user.getUserId() + "删除信件，信件ID为：" + letter.getId());
				letter.setReceiveDeleteFlag(1);

				if (letter.getSendDeleteFlag() == 1
						&& letter.getReceiveDeleteFlag() == 1) {
					// FIXME 信件不设外键，数据库须更改
					result = privateLetterMapper.deleteByPrimaryKey(id);
				} else {
					result = privateLetterMapper.updateByPrimaryKey(letter);
				}

				// 删除的是最新消息，则需将第二新的消息的发送显示状态更新为1
				if (letter.getReceiveShowFlag() == 1) {
					letter.setReceiveShowFlag(0);
					PrivateLetter latestLetter = privateLetterMapper
							.selectLatestLetter(user.getUserId(),
									letter.getSendId());
					if (latestLetter != null) {
						if (latestLetter.getSendId() == user.getUserId()) {
							latestLetter.setSendShowFlag(1);
						} else {
							latestLetter.setReceiveShowFlag(1);
						}
						privateLetterMapper.updateByPrimaryKey(latestLetter);
					}
				}
			}

			return result;

		} else {
			sysLogService.insertSysLog(LogConstants.OP_TYPE_DELETE, "阅读信件异常",
					"未找到相应的信件，可能已被删除。");
			return 0;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#insertPrivateLetter
	 * (com.tietiao.dao.model.PrivateLetter)
	 */
	@Override
	public int insertPrivateLetter(PrivateLetter letter) {
		sysLogService.insertSysLog(LogConstants.OP_TYPE_INSERT, "发送私信",
				letter.getSendId() + "发送私信给：" + letter.getReceiveId());
		return privateLetterMapper.insert(letter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#loadPrivateLetterId
	 * (int)
	 */
	@Override
	public PrivateLetter loadPrivateLetterId(int id) {
		return privateLetterMapper.selectByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#updateToRead(int)
	 */
	@Override
	public int updateToRead(int letterId) {
		PrivateLetter letter = loadPrivateLetterId(letterId);
		if (letter != null) {
			letter.setIsView(1);
			letter.setViewTime(new Date());
			privateLetterMapper.updateByPrimaryKey(letter);
			sysLogService.insertSysLog(LogConstants.OP_TYPE_UPDATE, "阅读信件",
					letter.getReceiveId() + "阅读了自己的信件，信件ID为：" + letter.getId());
			return 1;
		} else {
			sysLogService.insertSysLog(LogConstants.OP_TYPE_INSERT, "阅读信件异常",
					"未找到相应的信件，可能已被删除。");
			return 0;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#loadPrivateLetters
	 * (int)
	 */
	@Override
	public List<PrivateLetter> loadPrivateLetters(int userId) {
		privateLetterMapper.updateViewFlag(userId, new Date());
		return privateLetterMapper.selectPrivateLettersByUserId(userId, null,
				null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#sendPrivateLetter
	 * (int, int, java.lang.String)
	 */
	@Override
	public PrivateLetter sendPrivateLetter(int sendUserId, int receiveUserId,
			String content) {
		privateLetterMapper.updateShowFlag(0, 0, sendUserId, receiveUserId);
		PrivateLetter privateLetter = new PrivateLetter();
		privateLetter.setSendId(sendUserId);
		privateLetter.setReceiveId(receiveUserId);
		privateLetter.setContents(content);
		privateLetter.setSendTime(new Date());
		privateLetter.setSendShowFlag(1);
		privateLetter.setReceiveShowFlag(1);
		privateLetter.setIsView(0);
		privateLetter.setSendDeleteFlag(0);
		privateLetter.setReceiveDeleteFlag(0);
		privateLetterMapper.insert(privateLetter);
		return privateLetter;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.privateletter.PrivateLetterService#
	 * loadPrivateLetterDetails(int, int)
	 */
	@Override
	public List<PrivateLetter> loadPrivateLetterDetails(int loginUserId,
			int friendUserId) {
		return privateLetterMapper.selectPrivateDetails(loginUserId,
				friendUserId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#getNoViewCounts
	 * (int)
	 */
	@Override
	public int getNoViewCounts(int loginUserId) {
		return privateLetterMapper.selectNoViewLetterCounts(loginUserId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.privateletter.PrivateLetterService#deletePrivateLetters
	 * (int, int)
	 */
	@Override
	public synchronized int deletePrivateLetters(int loginUserId,
			int friendUserId) {
		int delNum = privateLetterMapper.deleteLetterByFriendId(loginUserId,
				friendUserId);
		int upNum = privateLetterMapper.updateDeleteFlags(loginUserId,
				friendUserId);
		return delNum + upNum;
	}

}
